Repository navigation
Conversation
## Motivation and Context The `subscriptions/listen` registry is per-process: a stream lives in the worker that accepted it, so a change published in another worker (Puma clustered mode, or several instances behind a load balancer) never reached it. This was the one remaining piece of cross-process state on the modern lifecycle; sessions, SSE resumability, and server-initiated requests are gone as of MCP 2026-07-28, and the MRTR pattern is specified to work "without requiring a shared storage layer across server instances". Both reference SDKs close the gap with the same seam - the Python SDK's `SubscriptionBus` protocol and the TypeScript SDK's `ServerEventBus` interface, each with an in-memory default and user-supplied brokers - and this ports that seam. `MCP::Server::SubscriptionBus` carries the four typed change events of `MCP::Subscriptions` (`ToolsListChanged`, `PromptsListChanged`, `ResourcesListChanged`, and `ResourceUpdated` with its URI), never JSON-RPC: acknowledgements, per-stream filter matching, and subscriptionId stamping stay in the transport, so a bus implementation can only move events between processes. `StreamableHTTPTransport` publishes every change through the bus and feeds its local streams from a bus listener registered at construction, making the arrival path identical for local and remote events; the in-process default publishes back synchronously, preserving the single-process delivery behavior. A multi-process deployment passes `subscription_bus:` backed by its own pub/sub system; the docs show a Redis sketch, and no broker adapter is bundled, matching both reference SDKs. `MCP::Subscriptions` is registered in the autoload list of `mcp.rb` like every other public constant, so a custom bus that rebuilds events from its broker at boot resolves the classes before any transport exists. A `publish` that raises, a broker outage, say, is reported through the exception reporter the way a raising listener or unsubscribe already is, so the legacy streams and modern sinks of the publishing worker still receive the change; the change senders rescue on their own, so what such a failure used to cost was that local delivery. The typed round trip carries only what the specification's params hold, the event kind and the resource URI, so an extension member a caller adds to one of the four change notifications reaches the legacy streams and modern sinks but not the listen streams, as in the Python SDK. The docs state that the default bus belongs to one transport, and the Redis sketch rescues per listener, like the default bus, with a separate rescue for a message the worker cannot decode. The page also states the contract a custom bus honors (an idempotent unregistering callable, listeners run outside the bus's locks on the receiving thread, and one already selected may still run after unregistering, which each stream's closed check absorbs), that with the default bus delivery is synchronous on the calling thread, so a stalled stream delays the notifier and a broker round trip in `publish` belongs behind a timeout, that the broker needs the same trust as the server processes, and what a production adapter adds to the sketch: waiting for the subscription before serving traffic, supervising the reader, stopping it on close, and building the bus after the fork. The class comment carries the same contract, and the `close` comment attributes the teardown ordering to each stream's write mutex and closed flag rather than to the unsubscribe. The transport does not depend on a custom bus honoring that contract. Its listener does nothing once the transport is closed or could not be built, so a listener left on the bus by an unregistering callable that raises, or by a `subscribe` that raises after registering, writes to no stream. An exception reporter that raises while one of the bus failures is reported is ignored, since the exception would end the reader thread of a broker, stop `close`, or cost the other streams of the worker the change. ## How Has This Been Tested? With the new bus unit tests and transport tests (including two transports sharing one bus standing in for two workers), the full suite, RuboCop, and the conformance suite, all green; the docs preview builds with every internal link and anchor check passing. A subprocess test resolves `MCP::Subscriptions` right after `require "mcp"`, and a bus whose `publish` raises is reported under its own context while a legacy GET stream still receives the change; both fail against the previous tree. Six more tests keep a listener on a bus whose unregistering callable or `subscribe` raises and report through a reporter that raises; each fails without the guard it covers. ## Breaking Changes None. The bus is a new optional keyword argument, and the in-process default preserves the existing single-process delivery behavior, apart from the extension members noted above, which no longer reach the listen streams.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The
subscriptions/listenregistry is per-process: a stream lives in the worker that accepted it, so a change published in another worker (Puma clustered mode, or several instances behind a load balancer) never reached it. This was the one remaining piece of cross-process state on the modern lifecycle; sessions, SSE resumability, and server-initiated requests are gone as of MCP 2026-07-28, and the MRTR pattern is specified to work "without requiring a shared storage layer across server instances". Both reference SDKs close the gap with the same seam - the Python SDK'sSubscriptionBusprotocol and the TypeScript SDK'sServerEventBusinterface, each with an in-memory default and user-supplied brokers - and this ports that seam.MCP::Server::SubscriptionBuscarries the four typed change events ofMCP::Subscriptions(ToolsListChanged,PromptsListChanged,ResourcesListChanged, andResourceUpdatedwith its URI), never JSON-RPC: acknowledgements, per-stream filter matching, and subscriptionId stamping stay in the transport, so a bus implementation can only move events between processes.StreamableHTTPTransportpublishes every change through the bus and feeds its local streams from a bus listener registered at construction, making the arrival path identical for local and remote events; the in-process default publishes back synchronously, preserving the single-process delivery behavior. A multi-process deployment passessubscription_bus:backed by its own pub/sub system; the docs show a Redis sketch, and no broker adapter is bundled, matching both reference SDKs.MCP::Subscriptionsis registered in the autoload list ofmcp.rblike every other public constant, so a custom bus that rebuilds events from its broker at boot resolves the classes before any transport exists. Apublishthat raises, a broker outage, say, is reported through the exception reporter the way a raising listener or unsubscribe already is, so the legacy streams and modern sinks of the publishing worker still receive the change; the change senders rescue on their own, so what such a failure used to cost was that local delivery. The typed round trip carries only what the specification's params hold, the event kind and the resource URI, so an extension member a caller adds to one of the four change notifications reaches the legacy streams and modern sinks but not the listen streams, as in the Python SDK. The docs state that the default bus belongs to one transport, and the Redis sketch rescues per listener, like the default bus, with a separate rescue for a message the worker cannot decode. The page also states the contract a custom bus honors (an idempotent unregistering callable, listeners run outside the bus's locks on the receiving thread, and one already selected may still run after unregistering, which each stream's closed check absorbs), that with the default bus delivery is synchronous on the calling thread, so a stalled stream delays the notifier and a broker round trip inpublishbelongs behind a timeout, that the broker needs the same trust as the server processes, and what a production adapter adds to the sketch: waiting for the subscription before serving traffic, supervising the reader, stopping it on close, and building the bus after the fork. The class comment carries the same contract, and theclosecomment attributes the teardown ordering to each stream's write mutex and closed flag rather than to the unsubscribe.The transport does not depend on a custom bus honoring that contract. Its listener does nothing once the transport is closed or could not be built, so a listener left on the bus by an unregistering callable that raises, or by a
subscribethat raises after registering, writes to no stream. An exception reporter that raises while one of the bus failures is reported is ignored, since the exception would end the reader thread of a broker, stopclose, or cost the other streams of the worker the change.How Has This Been Tested?
With the new bus unit tests and transport tests (including two transports sharing one bus standing in for two workers), the full suite, RuboCop, and the conformance suite, all green; the docs preview builds with every internal link and anchor check passing. A subprocess test resolves
MCP::Subscriptionsright afterrequire "mcp", and a bus whosepublishraises is reported under its own context while a legacy GET stream still receives the change; both fail against the previous tree. Six more tests keep a listener on a bus whose unregistering callable orsubscriberaises and report through a reporter that raises; each fails without the guard it covers.Breaking Changes
None. The bus is a new optional keyword argument, and the in-process default preserves the existing single-process delivery behavior, apart from the extension members noted above, which no longer reach the listen streams.
Types of changes
Checklist